<?php
namespace App\Services;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

/**
 * 阿狸短信
 */
class AliSms
{
  
  public static  function sendMsg($mobile,$code)
  {
    $parm = json_encode(['code'=>$code]);
    AlibabaCloud::accessKeyClient('LTAIZ1VWEmwtCHvw', 'GDF21nwoKoWZi5gNaPu7YMTUZ0R67q')
                            ->regionId('cn-hangzhou') // replace regionId as you need
                            ->asDefaultClient();

    try {
        $result = AlibabaCloud::rpc()
                              ->product('Dysmsapi')
                              // ->scheme('https') // https | http
                              ->version('2017-05-25')
                              ->action('SendSms')
                              ->method('POST')
                              ->host('dysmsapi.aliyuncs.com')
                              ->options([
                                          'query' => [
                                            'RegionId' => "default",
                                            'PhoneNumbers' => $mobile,
                                            'SignName' => "太保互动",
                                            'TemplateCode' => "SMS_135590001",
                                            'TemplateParam'=>$parm,
                                          ],
                                        ])
                              ->request();
        return $result->toArray();
    } catch (ClientException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    } catch (ServerException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    }
  }

  public static function QuerySmsTemplate()
  {
    AlibabaCloud::accessKeyClient('LTAIZ1VWEmwtCHvw', 'GDF21nwoKoWZi5gNaPu7YMTUZ0R67q')
                            ->regionId('cn-hangzhou') // replace regionId as you need
                            ->asDefaultClient();

    try {
        $result = AlibabaCloud::rpc()
                              ->product('Dysmsapi')
                              // ->scheme('https') // https | http
                              ->version('2017-05-25')
                              ->action('QuerySmsTemplate')
                              ->method('POST')
                              ->host('dysmsapi.aliyuncs.com')
                              ->options([
                                          'query' => [
                                            'RegionId' => "default",
                                           
                                            'TemplateCode' => "SMS_135590001",
                                          ],
                                        ])
                              ->request();
        print_r($result->toArray());
    } catch (ClientException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    } catch (ServerException $e) {
        echo $e->getErrorMessage() . PHP_EOL;
    }
  }
  
}


